/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.tramite.be.DocRequerido;
import org.cip.esymons.tramite.be.Procedimiento;
import org.cip.esymons.tramite.be.Tramite;

/**
 *
 * @author ChristianATE
 */
@Stateless
public class DocRequeridoFacade implements DocRequeridoFacadeLocal {
    
    @PersistenceContext
    private EntityManager em;

    public void create(DocRequerido docRequerido) {
        em.persist(docRequerido);
    }

    public void edit(DocRequerido docRequerido) {
        em.merge(docRequerido);
    }

    public void remove(DocRequerido docRequerido) {
        em.remove(em.merge(docRequerido));
    }

    public DocRequerido find(Object id) {
        return em.find(DocRequerido.class, id);
    }

    public List<DocRequerido> findAll() {
        return em.createQuery("select object(o) from DocRequerido as o").getResultList();
    }

    public List<DocRequerido> listarDocumentoXProcedimiento(Procedimiento proc) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append(" select d");
            sb.append(" from DocRequerido d inner Join d.fkProCodigo p");
            sb.append(" where p=?1");

            q = em.createQuery(sb.toString());

            q.setParameter(1, proc);

            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public List<DocRequerido> listarDocumentoXTramite(Tramite tramite) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append(" select d");
            sb.append(" from DocRequerido d inner Join d.fkProCodigo p");
            sb.append(" where p.fkTrmCodigo=?1 and p.proNombre=?2");

            q = em.createQuery(sb.toString());

            q.setParameter(1, tramite);
            q.setParameter(2, "Inicio");

            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }
}
